require(['grid','select2','file1'], function () {
    var editDialog,flag,strtusDateHtml,selLineId,selCorpId;
    $('form').initForm();
    $('#upStnLine,#downStnLine').stationChart();
		//左侧树
		commonFun.leftTreeNodeClickObj['sysLineSiteSet'] = function (event, treeId, treeNode) {
			if(treeNode.type==='LINE'){
				selLineId=treeNode.id;
				selCorpId=treeNode.corpId;
				$.ajaxExt({
					url :"/stnLineRel/getStrtusDate.htm",
					data : {'lineId' : selLineId}
				}).done(function(result){
					strtusDateHtml="";
					$(result).each(function(i,item) {
						strtusDateHtml+='<option value="'+item.strtusDate+'"'+(i===0?"selected":"")+'>'+item.strtusDate+'</option>';
					});
					if(treeNode.type=="LINE"){
						$("#lineText").html(treeNode.name);
					}else{
						$("#lineText").html("");
					}
					$("#initStrtusDate").html(strtusDateHtml);
					if(strtusDateHtml){
						$("#initStrtusDate").trigger('change');
					} 
					initLineSite(selLineId);
				});
			}else{
				$.messager.alert("提示","请选择一条线路进行查看。");
			}
		};
		
		//弹出层控制
		$("#addStnLineRel").click(function() {
			if(selLineId){
				getEditDialog('add','新增').dialog('exec', 'addInit',strtusDateHtml,selCorpId);
			}else{
				$.messager.alert("提示","请选择一条线路进行查看。");
			}
		});
		
		$("#editStnLineRel").click(function() {
			if(selLineId){
				getEditDialog('edit','修改').dialog('exec', 'editInit',$("#initStrtusDate").val(),strtusDateHtml,selCorpId);
			}else{
				$.messager.alert("提示","请选择一条线路进行查看。");
			}
		});
		
		/**
	     * 获取表单对话框，如果没有就先创建
	     * @param option {object} 对话框配置
	     * @returns {jQuery}
	     */
	    function getEditDialog(flag2,option) {
	    	flag =flag2;
	        option = option || {};
	        if (editDialog) {
	            editDialog.dialog('open');
	            if (option.title) {
	                editDialog.dialog('setOption', 'title', option.title);
	            }
	            return editDialog;
	        } else {
	            editDialog = $('<div>').dialog({
	                title: option.title || '线路站点分配',
	                url: './master/setting/sysLineSiteSetEdit.htm',
	                height: 640,
	                width: 880,
	                autoOpen: true,
	                buttons: [
	                    {
	                        text: '确定',
	                        css: 'mr10',
	                        click: function () {
	                        	if(flag=='add'){
	                        		editDialog.dialog('exec', 'add',function(){
	                        			saveData();
	                        			$.messager.alert("提示","您成功添加了一条线路站点。","success");
	                    		    	getEditDialog().dialog('close');
	                    		    });
	                        	}else if(flag=='edit'){
	                        		editDialog.dialog('exec', 'edit',function(){
	                        			saveData();
	                        			$.messager.alert("提示","保存成功。","success");
	                        			getEditDialog().dialog('close');
	                        		});
	                        	}
	                        }
	                    },
	                    {
	                        text: '关闭',
	                        css: 'btn-cancel',
	                        click: function () {
	                            $(this).dialog('close');
	                        }
	                    }
	                ]
	            });
	            return editDialog;
	        }
	    }
	    
    //点击线路站点启用日期查询站点信息
	$("#initStrtusDate").on('change',function(){
		var nodes=commonFun.getLeftTreeSelectedNodes();
		if(nodes[0].type==='LINE'){
			selLineId=nodes[0].id;
		}
		initLineSite(selLineId);
	});
    //删除版本
	$("#delStnLineRel").click(function(){
		var nodes=commonFun.getLeftTreeSelectedNodes();
		if(nodes.length!=1){
			$.messager.alert("提示","只能选择一条线路");
		}else{
			var selLineId;
			var selLineName;
			if(nodes[0].type==='LINE'){
				selLineId=nodes[0].id;
			}if(nodes[0].type==='LINE'){
				selLineName=nodes[0].name;
			}
			var strtusdate = $("#initStrtusDate").val();
			strtusdate=strtusdate.replace("-", "/").replace("-", "/");
			var d = new Date();
			var currentDate="";
			var yyyy = d.getFullYear();
			var mm = d.getMonth()+1;
			var dd = d.getDate();
			mm = mm>9?mm.toString():'0' + mm;
			dd = dd>9?dd.toString():'0' + dd;
			currentDate=yyyy + "-" + mm + "-" + dd;
			var systemDate=currentDate.replace("-", "/").replace("-", "/");
			if(strtusdate<systemDate){
				$.messager.alert("提示","历史版本不能删除!");
			}else{
				var initStrtusDate = $("#initStrtusDate").val();
				$.messager.confirm("是否删除","线路:"+selLineName+" 将解除启用日期为:"+initStrtusDate+"的线路站点信息,确实解除吗?",function(r){
					if(r){
						$.ajaxExt({
							url:"/stnLineRel/deleteStnLineRel.htm",
							data:{"lineId":selLineId,"strtusDate":initStrtusDate}
						}).done(function(){
							$.ajaxExt({
								url :"/stnLineRel/getStrtusDate.htm",
								data : {'lineId' : selLineId}
							}).done(function(result){
								strtusDateHtml="";
								$(result).each(function(i,item) {
									strtusDateHtml+='<option value="'+item.strtusDate+'"'+(i===0?"selected":"")+'>'+item.strtusDate+'</option>';
								});
								$("#initStrtusDate").html(strtusDateHtml);
								if(strtusDateHtml){
									$("#initStrtusDate").trigger('change');
								} 
								initLineSite(selLineId);
							});
						});
					}
				});
			}
		}
	});
	
});

	//初始化线路数据
	function initLineSite(selLineId){
		$.ajaxExt({
			url:"/stnLineRel/getLineInfo.htm",
			data:{"lineId":selLineId}
		}).done(function(result){
			if(result.upstreFirstTime==undefined){
				$("[name='upstreFirstTime']").html("首车：-");
			}else{
				$("[name='upstreFirstTime']").html("首车："+ result.upstreFirstTime);
			}
			if(result.upstreFirstTime==undefined){
				$("[name='upstreLastTime']").html("末车：-");
			}else{
				$("[name='upstreLastTime']").html("末车："+ result.upstreLastTime);
			}
			if(result.upstreLastTime=="" ||result.upstreLastTime =="0" ||result.upstreLastTime ==undefined){
				$("[name='throoutPrice']").html("票价：-");
			}else{
				$("[name='throoutPrice']").html("票价：最高票价："+ result.upstreLastTime +"元");
			}
		})
//		$("#lineStnEnableData").html("线路站点启用日期："+ $("#initStrtusDate").val());
		//上行
		$("#upStnLine").stationChart('load',"/stnLineRel/getUpStnLineRelByLineId.htm",
			{"lineId":selLineId,"strtusDate":$("#initStrtusDate").val()},function(data){
				var beginStatin_Line="",endStatin_Line="",upStnWidth;
				$(data).each(function(i,item) {
					upStnWidth=i;
					if(i==0){
						beginStatin_Line=item.stnName;
					}
					endStatin_Line=item.stnName;
				});
				upStnWidth=(upStnWidth+1)*27+15;
				$(".bus-stop-list").eq(0).css("width",upStnWidth+"px");
				$("#beginStatin_Line").html(beginStatin_Line.replace(/(︹)(\S+)(︺)/,'($2)'));
				$("#endStatin_Line").html(endStatin_Line.replace(/(︹)(\S+)(︺)/,'($2)'));
			});
		//下行
		$("#downStnLine").stationChart('load',"/stnLineRel/getDownStnLineRelByLineId.htm",
			{"lineId":selLineId,"strtusDate":$("#initStrtusDate").val()},function(data){
				var beginStatin_Line_down="",endStatin_Line_down="",downStnWidth;
				$(data).each(function(i,item) {
					downStnWidth=i;
					if(i==0){
						beginStatin_Line_down=item.stnName;
					}
					endStatin_Line_down=item.stnName;
				});
				downStnWidth=(downStnWidth+1)*27+15;
				$(".bus-stop-list").eq(1).css("width",downStnWidth+"px");
				$("#beginStatin_Line_down").html(beginStatin_Line_down.replace(/(︹)(\S+)(︺)/,'($2)'));
				$("#endStatin_Line_down").html(endStatin_Line_down.replace(/(︹)(\S+)(︺)/,'($2)'));
			});
		
	}
	
	function saveData(){
		var nodes=commonFun.getLeftTreeSelectedNodes();
  		var selLineId;
  		var selLineName;
  		if(nodes[0].type==='LINE'){
  			selLineId=nodes[0].id;
  			selLineName=nodes[0].name;
  			$("#lineText").html(selLineName);
  		}else{
			$("#lineText").html("");
		}
  		$.ajaxExt({
			url :"/stnLineRel/getStrtusDate.htm",
			data : {'lineId' : selLineId}
		}).done(function(result){
			var strtusDateHtml="";
			$(result).each(function(i,item) {
				strtusDateHtml+='<option value="'+item.strtusDate+'"'+(i===0?"selected":"")+'>'+item.strtusDate+'</option>';
			});
			$("#initStrtusDate").html(strtusDateHtml);
			if(strtusDateHtml){
				$("#initStrtusDate").trigger('change');
			} 
			initLineSite(selLineId);
		});
	}
